package com.xiaomei.manager.utils;

import org.apache.log4j.Logger;

import com.aliyun.oss.OSSClient;

public class OSSClientUtils {
	
	private static final Logger logger = Logger.getLogger(OSSClientUtils.class);
	
	private OSSClientUtils() {}
	
	private static OSSClient ossClient = null;
	static {
		// endpoint以杭州为例，其它region请按实际情况填写
		String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
		// accessKey请登录https://ak-console.aliyun.com/#/查看
		String accessKeyId = "rUNUcDWgnxUDs3gr";
		String accessKeySecret = "OyC9b9o7zKdp6ulS9XIhe2r1nLe79z";

		// 创建OSSClient实例
		ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
	}
	
	public static OSSClient getInstance() {
		return ossClient;
	}
	
	/**
	 * 删除OSS对象
	 * @param bucketName
	 * @param key
	 * @return
	 */
	public static boolean removeOSSObject(String bucketName, String key) {
		try {
			// 删除Object
			ossClient.deleteObject(bucketName, key);
			
			// 关闭client
//			ossClient.shutdown();
			
			return true;
		} catch(Exception e) {;
			logger.error("删除OSS对象失败", e);
			return false;
		}
	}
	
	/**
	 * 获取oss访问地址
	 */
	public static String getOSSUrl(String bucketName, String fileName) {
		return "http://"+ bucketName +".oss-cn-hangzhou.aliyuncs.com/" + fileName;
	}
	
	public static String getSuffixByFilename(String filename) {
        String extName = "";
        if (filename != null) {
            if ((filename != null) && (filename.indexOf(".") != -1)) {
                extName = filename.substring(filename.lastIndexOf(".") + 1);
            }
        }
        return extName.toLowerCase();
    }
	
	public static void main(String[] args) {
		System.out.println(getSuffixByFilename("aaa.jpg"));
		System.out.println("aaa.jpg".substring("aaa.jpg".lastIndexOf("."), "aaa.jpg".length()));
	}
}
